<?php

require 'base.php';

class Oxwall extends driver_base {

    public function __construct($db) {
        //parent::__construct();
        $this->db = $db;
    }

//------------------------------------------------------------------------------
    public function getDBdata($session_id, $first) {

        if ($session_id == null) {
            $_SESSION[$this->uid . 'is_guest'] = 1;
        } else {
            $_SESSION[$this->uid . 'is_guest'] = 0;
        }

        if (!$_SESSION[$this->uid . 'is_guest']) {
            //if user

            if ($_SESSION[$this->uid . 'time'] < $this->online_time || isset($_SESSION[$this->uid . 'usr_name']) == false || $first == 'false') { //To consume less resources , now the query is made only once in 15 seconds
                //run every 15 seconds or run on first load
                if ($this->displayname == 'username') {
                    $query = "SELECT DISTINCT username FROM " . DBprefix . "base_user 
                                 WHERE id = ?  LIMIT 1";
                } else if ($this->displayname == 'name') {
                    $query = "SELECT DISTINCT questionName as index , textValue AS value FROM " . DBprefix . "base_question_data
                                 WHERE userId = ?";
                }


                $res_obj = $this->db->prepare($query);
                $res_obj->execute(array($session_id)); // var_dump($res_obj);
                $res = $res_obj->fetchAll();

                if ($res == null) {
                    $this->freichat_debug("Incorrect Query :  " . $query . " \n session id:  " . $session_id . "\n PDO error: " . print_r($this->db->errorInfo(), true));
                }

                foreach ($res as $result) {

                    if ($this->displayname == 'username') {
                        if (isset($result['username'])) {
                            $_SESSION[$this->uid . 'usr_name'] = $result['username'];
                            $_SESSION[$this->uid . 'usr_ses_id'] = $session_id;
                        }
                    } else if ($this->displayname == 'name') {
                        if (isset($result['questionName']) && $result['questionName'] == 'realname') {
                            $_SESSION[$this->uid . 'usr_name'] = $result['textValue'];
                            $_SESSION[$this->uid . 'usr_ses_id'] = $session_id;
                        }
                    }
                }
            }
        } else {
            $_SESSION[$this->uid . 'usr_name'] = $_SESSION[$this->uid . 'gst_nam'];
            $_SESSION[$this->uid . 'usr_ses_id'] = $_SESSION[$this->uid . 'gst_ses_id'];
        }
    }

//------------------------------------------------------------------------------ 
    public function avatar_url($res) {

        $murl = str_replace($this->to_freichat_path, "", $this->url);
        $avatar_url = $murl . 'ow_userfiles/plugins/base/avatars/avatar_' . $res['session_id'] . '_' . $res[$this->avatar_field_name] . '.jpg';
        return $avatar_url;
    }

//------------------------------------------------------------------------------
    public function getList() {

        $user_list = null;

        if ($this->show_name == 'guest') {
            $user_list = $this->get_guests();
        } else if ($this->show_name == 'user') {
            $user_list = $this->get_users();
        } else {
            $this->freichat_debug('USER parameters for show_name are wrong.');
        }
        return $user_list;
    }

//------------------------------------------------------------------------------   
    public function get_guests() {

        $query = "SELECT DISTINCT f.status_mesg,f.username,f.session_id,f.status,f.guest AS is_guest,f.in_room,a.hash AS avatar
                   FROM frei_session AS f
                   LEFT JOIN ".DBprefix."base_avatar AS a ON a.userId = f.session_id
                   WHERE f.time>" . $this->online_time2 . "
                   AND f.session_id<>" . $_SESSION[$this->uid . 'usr_ses_id'] . "
                   AND f.status<>2
                   AND f.status<>0
                   ";

        $list = $this->db->query($query)->fetchAll();

        return $list;
    }

//------------------------------------------------------------------------------   
    public function get_users() {

        $query = "SELECT DISTINCT f.status_mesg,f.username,f.session_id,f.status,f.guest AS is_guest,f.in_room,a.hash AS avatar
                   FROM frei_session AS f
                   LEFT JOIN ".DBprefix."base_avatar AS a ON a.userId = f.session_id
                   WHERE f.time>" . $this->online_time2 . "
                   AND f.session_id<>" . $_SESSION[$this->uid . 'usr_ses_id'] . "
                   AND f.status<>2
                   AND f.status<>0
                   AND f.guest=0
                   ";
//echo $query;

        $list = $this->db->query($query)->fetchAll();
        return $list;
    }
//------------------------------------------------------------------------------   
    public function get_buddies() {

        $query = "SELECT DISTINCT f.status_mesg,f.username,f.session_id,f.status,f.guest AS is_guest,f.in_room,a.hash AS avatar
                   FROM frei_session AS f
                   LEFT JOIN ".DBprefix."base_avatar AS a ON a.userId = f.session_id
                   LEFT JOIN ".DBprefix."friends_friendship AS r ON r.userId = f.session_id OR r.friendId = f.session_id
                   WHERE f.time>" . $this->online_time2 . "
                   AND f.session_id<>" . $_SESSION[$this->uid . 'usr_ses_id'] . "
                   AND f.status<>2
                   AND f.status<>0
                   AND f.guest=0
                   AND r.status='active'
                   ";
//echo $query;

        $list = $this->db->query($query)->fetchAll();
        return $list;
    }

//------------------------------------------------------------------------------ 
    public function load_driver() {

        //define("DBprefix", $this->db_prefix);
        $session_id = $this->options['id'];
        $custom_mesg = $this->options['custom_mesg'];
        $first = $this->options['first'];

// 1. Connect The DB
//      DONE
// 2. Basic Build the blocks        
        $this->createFreiChatXsession();
// 3. Get Required Data from client DB
        $this->getDBdata($session_id, $first);
        $this->check_ban();
// 4. Insert user data in FreiChatX Table Or Recreate Him if necessary
        $this->createFreiChatXdb();
// 5. Update user data in FreiChatX Table
        $this->updateFreiChatXdb($first, $custom_mesg);
// 6. Delete user data in FreiChatX Table
        $this->deleteFreiChatXdb();
// 7. Get Appropriate UserData from FreiChatX Table
        if ($this->usr_list_wanted) {
            $result = $this->getList();
            return $result;
        }
// 8. Send The final Data back
        return true;
    }

}